import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wch/model/flag_model.dart';
import 'package:wch/utils/color_utils.dart';
import 'package:wch/utils/log_utils.dart';

import '../constant/resource_constant.dart';
import '../utils/text_utils.dart';
import '../widget/text_widget.dart';

class FlagsWidget {
  ///callback 返回整体的高度
  static List<Widget> getFlagsWidget(List<String> flags, double maxWidth, double maxHeight, double fontSize, Function callback) {
    List<Widget> widgets = [Container()];

    double widgetHeight = 0;
    double posX = 0;
    for (int i = 0; i < flags.length; i++) {
      if (flags[i] == '') {
        continue;
      }
      Size size = _getTextSize(flags[i], maxWidth, fontSize);
      if (widgetHeight == 0) {
        widgetHeight = size.height; //10 代表上下边距
      }
      posX += size.width + 10;
      if (posX > maxWidth) {
        posX = size.width + 10;
        widgetHeight += size.height + 2.5; //加上外边距和下一行的高度
      }

      if (widgetHeight > maxHeight) {
        break;
      }

      widgets.add(Positioned(
        left: posX - size.width - 10,
        top: widgetHeight - size.height,
        width: size.width + 10,
        height: size.height,
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: Color(0xFFFFDCE8),
              borderRadius: BorderRadius.all(Radius.circular(7.5.w))),
          // child: F19TitleText(
          //   flags[i],
          //   color: Color(0xFFDE5385),
          // ),
          child: MyText(flags[i], color: Color(0xFFDE5385), fontSize: fontSize),
        ),
      ));

      posX += 5;
    }
    callback(widgetHeight);
    return widgets;
  }

  static List<Widget> getFlagsWidget2(List<FlagModel> flags, double maxWidth, double maxHeight, double fontSize, Function callback) {
    List<Widget> widgets = [Container()];

    double widgetHeight = 0;
    double posX = 0;
    for (int i = 0; i < flags.length; i++) {
      Size size = _getTextSize(flags[i].flagName, maxWidth, fontSize);
      if (widgetHeight == 0) {
        widgetHeight = size.height; //10 代表上下边距
      }
      posX += size.width + 10;
      if (posX > maxWidth) {
        posX = size.width + 10;
        widgetHeight += size.height + 2.5; //加上外边距和下一行的高度
      }

      if (widgetHeight > maxHeight) {
        break;
      }

      widgets.add(Positioned(
        left: posX - size.width - 10,
        top: widgetHeight - size.height,
        width: size.width + 10,
        height: size.height,
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: flags[i].backgroundColor!,
              borderRadius: BorderRadius.all(Radius.circular(7.5.w))),
          // child: F19TitleText(
          //   flags[i],
          //   color: Color(0xFFDE5385),
          // ),
          child: MyText(flags[i].flagName, color: flags[i].textColor!, fontSize: fontSize),
        ),
      ));

      posX += 5;
    }
    callback(widgetHeight);
    return widgets;
  }

  static List<Widget> getFlagsWidgetAndHead(Widget headWidget, double headWidth, List<String> flags, double maxWidth, double maxHeight, double fontSize, Function callback) {
    List<Widget> widgets = [Container(), Positioned(child: headWidget, left: 0, top: 10, width: headWidth,)];

    double widgetHeight = 10;
    double posX = headWidth;
    for (int i = 0; i < flags.length; i++) {
      Size size = _getTextSize(flags[i], maxWidth, fontSize);
      if (widgetHeight == 10) {
        widgetHeight = size.height + 10; //10 代表上下边距
      }
      posX += size.width + 10;
      if (posX > maxWidth) {
        posX = size.width + 10;
        widgetHeight += size.height + 7.5; //加上外边距和下一行的高度
      }

      if (widgetHeight > maxHeight) {
        break;
      }

      widgets.add(Positioned(
        left: posX - size.width - 10,
        top: widgetHeight - size.height,
        width: size.width + 10,
        height: size.height,
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: Color(0xFFFFDCE8),
              borderRadius: BorderRadius.all(Radius.circular(7.5.w))),
          // child: F19TitleText(
          //   flags[i],
          //   color: Color(0xFFDE5385),
          // ),
          child: MyText(flags[i], color: Color(0xFFDE5385), fontSize: fontSize),
        ),
      ));

      posX += 5;
    }
    callback(widgetHeight);
    return widgets;
  }
  static List<Widget> getFlagsWidgetAndHead2(Widget headWidget, double headWidth, List<FlagModel> flags, double maxWidth, double maxHeight, double fontSize, Function callback) {
    List<Widget> widgets = [Container(), Positioned(child: headWidget, left: 0, top: 10, width: headWidth, height: 20,)];

    double widgetHeight = 10;
    double posX = headWidth;
    for (int i = 0; i < flags.length; i++) {
      Size size = _getTextSize(flags[i].flagName, maxWidth, fontSize);
      if (widgetHeight == 10) {
        widgetHeight = size.height + 10; //10 代表上下边距
      }
      posX += size.width + 10;
      if (posX > maxWidth) {
        posX = size.width + 10;
        widgetHeight += size.height + 7.5; //加上外边距和下一行的高度
      }

      if (widgetHeight > maxHeight) {
        break;
      }

      widgets.add(Positioned(
        left: posX - size.width - 10,
        top: widgetHeight - size.height,
        width: size.width + 10,
        height: size.height,
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: flags[i].backgroundColor,
              borderRadius: BorderRadius.all(Radius.circular(7.5.w))),
          // child: F19TitleText(
          //   flags[i],
          //   color: Color(0xFFDE5385),
          // ),
          child: MyText(flags[i].flagName, color: flags[i].textColor!, fontSize: fontSize),
        ),
      ));

      posX += 5;
    }
    if (widgetHeight == 10) {
      widgetHeight = 30.h;
    }
    callback(widgetHeight);
    return widgets;
  }

  static Size _getTextSize(String text, double maxWidth, double fontSize) {
    TextStyle style = TextStyle(
      fontSize: fontSize,
    );
    Size size = TextUtils.boundingTextSize(text, style,
        maxWidth: maxWidth);
    return Size(size.width+5, size.height+4);
  }

  ///callback 返回整体的高度
  static List<Widget> getEditableFlagsWidget(List<FlagModel> flags, double maxWidth, double maxHeight, double fontSize, Function callback) {
    List<Widget> widgets = [Container()];

    double totalHeight = 10;
    double posX = 0;
    for (int i = 0; i < flags.length; i++) {
      Size size = _getTextSize(flags[i].flagName, maxWidth, fontSize);
      if (totalHeight == 10) {
        totalHeight = size.height + 20;
      }
      posX += size.width + 20;
      if (posX > maxWidth) {
        posX = size.width + 20;
        totalHeight += size.height + 10;
      }

      if (totalHeight > maxHeight) {
        break;
      }

      widgets.add(Positioned(
        left: posX - size.width - 20,
        top: totalHeight - size.height - 20,
        width: size.width + 30,
        height: size.height+10,
        child: Stack(
          children: [
            GestureDetector(
              onTap: (){

              },
              child: Container(
                alignment: Alignment.center,
                width: size.width+20,
                margin: EdgeInsets.only(top: 10),
                decoration: BoxDecoration(
                    color: Color(0xFFFFDCE8),
                    borderRadius: BorderRadius.all(Radius.circular(7.5.w))),
                child: MyText(flags[i].flagName, color: Color(0xFFDE5385), fontSize: fontSize),
              ),
            ),
            Positioned(child: Offstage(offstage: !flags[i].isChecked, child: Image.asset(flagCheckedIcon, width: 13.w, height: 13.w,)), right: 5, top: 5,)
          ],
        ),
      ));

      posX += 10;
    }
    callback(totalHeight);
    return widgets;
  }
}